    //初始化界面
    $(function() {
        var listTitle = ""; //单据标题
        var listType = ""; //类型
        var listTypeEn = ""; //英文类型
        getType();
        getPersonList(); //获取业务员
        initTableData();
        ininPager();
        bindEvent();
    });

    //根据名称获取类型
    function getType(){
   
        listTitle = $("#tablePanel").prev().text();
        if(listTitle === "BOM 列表"){
            listType = "Bom";
            listTypeEn = "Vendor";
        }
    }

    //获取业务员
    function getPersonList(){
    
        $.ajax({
            type:"post",
            url: path + "/person/getBasicData.action",
            dataType: "json",
            success: function (systemInfo)
            {
                var msgTip = systemInfo.showModel.msgTip;
                if(msgTip !== "exceptoin"){
                    var personList = systemInfo.showModel.map.personList;
                    var personID,options;
                    if(personList !=null)
                    {
                        for(var i = 0 ;i < personList.length;i++)
                        {
                            var person = personList[i];
                            if(0 == i)
                            {
                                personID = person.id;
                            }
                            if(person.type=="仓管员")
                            {
                                options += '<option value="' + person.id + '">' + person.name + '</option>';
                            }
                        }
                        $("#personId").empty().append('<option>不关联业务员</option>').append(options);
                    }
                }
                else {
                    $.messager.alert('提示','查找系统基础信息异常,请与管理员联系！','error');
                    return;
                }
            }
        });
    }

    //初始化表格数据
    function initTableData() {
   
        //改变宽度和高度
        $("#searchPanel").panel({width:webW-2});
        $("#tablePanel").panel({width:webW-2});
        $('#tableData').datagrid({
            //title:'单位列表',
            //iconCls:'icon-save',
            //width:700,
            height:heightInfo,
            nowrap: false,
            rownumbers: false,
            //动画效果
            animate:false,
            //选中单行
            singleSelect : true,
            collapsible:false,
            selectOnCheck:false,
            //fitColumns:true,
            //单击行是否选中
            checkOnSelect : false,
            //交替出现背景
            striped : true,
            pagination: true,
            //自动截取数据
            //nowrap : true,
            //loadFilter: pagerFilter,
            pageSize: initPageSize,
            pageList: initPageNum,
            columns:[[
                { field: 'id',width:35,align:"center",checkbox:true},
                { title: '操作',field: 'op',align:"center",width:60,formatter:function(value,rec)
                    {
                        var str = '';
                        var rowInfo = rec.id + 'AaBb' + rec.item +'AaBb' + rec.qty + 'AaBb'+ rec.ref_designator + 'AaBb'+ rec.complete_part + 'AaBb'+ rec.description + 'AaBb'+ rec.pakage_type_or_doc_type + 'AaBb' + rec.value_or_rev + 'AaBb' + rec.voltage+ 'AaBb' + rec.tolerances
                            + 'AaBb' + rec.special_comments + 'AaBb' + rec.rohs + 'AaBb' + rec.temperature_range + 'AaBb' + rec.manufacturer + 'AaBb' + rec.supplier + 'AaBb' + rec.supplier_part_no + 'AaBb' + rec.second_source_manufacturer
                            + 'AaBb' + rec.second_source_part_no + 'AaBb' + rec.dr_rart_number;
                        if(1 == value)
                        {
                            str += '<img title="编辑" src="' + path + '/js/easyui-1.3.5/themes/icons/pencil.png" style="cursor: pointer;" onclick="editBom(\'' + rowInfo + '\');"/>&nbsp;&nbsp;&nbsp;';
                            str += '<img title="删除" src="' + path + '/js/easyui-1.3.5/themes/icons/edit_remove.png" style="cursor: pointer;" onclick="deleteBom(\'' + rowInfo + '\');"/>';
                            
                        }
                        return str;
                    }
                },
                { title: 'Item',field: 'item',width:50},
                { title: 'Qty.', field: 'qty',width:60,align:"center"},
                { title: 'Ref.Designator', field: 'ref_designator',width:140,align:"center"},
                { title: 'Complete Part No. or Doc.No',field: 'complete_part',width:160,align:"center"},
                { title: 'Description', field: 'description',width:170,align:"center"},
                { title: 'Pakage type or Doc Type', field: 'pakage_type_or_doc_type',width:120,align:"center"},
                { title: 'Value or Rev.',field: 'value_or_rev',width:170,align:"center"},
                { title: 'Voltage',field: 'voltage',width:100,align:"center"},
                { title: 'Tolerances',field: 'tolerances',width:50},
                { title: 'Special Comments',field: 'special_comments',width:130},
                { title: 'RoHS', field: 'rohs',width:60,align:"center"},
                { title: 'Temperature Range', field: 'temperature_range',width:140,align:"center"},
                { title: 'Manufacturer',field: 'manufacturer',width:160,align:"center"},
                { title: 'Supplier', field: 'supplier',width:170,align:"center"},
                { title: 'Supplier Part No.', field: 'supplier_part_no',width:120,align:"center"},
                { title: 'Second source Manufacturer',field: 'second_source_manufacturer',width:170,align:"center"},
                { title: 'Second source Part no.',field: 'second_source_part_no',width:100,align:"center"},
                { title: 'DR Part Number',field: 'dr_rart_number',width:100,align:"center"}
            ]],
            toolbar:[
                {
                    id:'addBom',
                    text:'增加',
                    iconCls:'icon-add',
                    handler:function() {
                        addBom();
                    }
                },'-',
                {
                    id:'deleteBom',
                    text:'删除',
                    iconCls:'icon-remove',
                    handler:function() {
                        batDeleteBom();
                    }
                },'-',
                {
                    id:'setInput',
                    text:'导入',
                    iconCls:'icon-excel',
                    handler:function() {
                    	setInputFun();
                    }
                },'-',
                {
                    id:'setOutput',
                    text:'导出',
                    iconCls:'icon-excel',
                    handler:function() {
                        setOutputFun();
                    }
                },'-',
                {
                    id:'getToken',
                    text:'Token',
                    iconCls:'icon-excel',
                    handler:function() {
                        getToken();
                    }
                }
                
                
            ],
            onLoadError:function()
            {
                $.messager.alert('页面加载提示','页面加载异常，请稍后再试！','error');
                return;
            }
        });
    }


    //分页信息处理
    function ininPager() {
    
        try
        {
            var opts = $("#tableData").datagrid('options');
            var pager = $("#tableData").datagrid('getPager');
    
            pager.pagination({
                onSelectPage:function(pageNum, pageSize)
                {
                	
                    opts.pageNumber = pageNum;             
                    opts.pageSize = pageSize;
                    pager.pagination('refresh',
                        {
                            pageNumber:pageNum,
                            pageSize:pageSize
                        });
       
                    showBomDetails(pageNum,pageSize);
                }
            });
        }
        catch (e)
        {
            $.messager.alert('异常处理提示',"分页信息异常 :  " + e.name + ": " + e.message,'error');
        }
    }

    //删除信息
    function deleteBom(BomInfo) {
        $.messager.confirm('删除确认','确定要删除此条信息吗？',function(r)
        {
            if (r)
            {
                var BomTotalInfo = BomInfo.split("AaBb");
                $.ajax({
                    type:"post",
                    url: path + "/bom/delete.action",
                    dataType: "json",
                    data: ({
                        BomID : BomTotalInfo[0],
                        Bom:BomTotalInfo[1],
                        clientIp: clientIp
                    }),
                    success: function (tipInfo)
                    {
                        var msg = tipInfo.showModel.msgTip;
                        if(msg == '成功')
                        //加载完以后重新初始化
                            $("#searchBtn").click();
                        else
                            $.messager.alert('删除提示','删除信息失败，请稍后再试！','error');
                    },
                    
                    //此处添加错误处理
                    error:function()
                    {
                        $.messager.alert('删除提示','删除信息异常，请稍后再试！','error');
                        return;
                    }
                });
            }
        });
    }

    //批量删除单位
    function batDeleteBom() {
        var row = $('#tableData').datagrid('getChecked');
        if(row.length == 0)
        {
            $.messager.alert('删除提示','没有记录被选中！','info');
            return;
        }
        if(row.length > 0)
        {
            $.messager.confirm('删除确认','确定要删除选中的' + row.length + '条信息吗？',function(r)
            {
                if (r)
                {
                    var ids = "";
                    for(var i = 0;i < row.length; i ++)
                    {
                        if(i == row.length-1)
                        {
                            ids += row[i].id;
                            break;
                        }
                        ids += row[i].id + ",";
                    }
                    $.ajax({
                        type:"post",
                        url: path + "/bom/batchDelete.action",
                        dataType: "json",
                        async :  false,
                        data: ({
                            BomIDs : ids,
                            clientIp: clientIp
                        }),
                        success: function (tipInfo)
                        {
                            var msg = tipInfo.showModel.msgTip;
                            if(msg == '成功')
                            {
                                //加载完以后重新初始化
                                $("#searchBtn").click();
                                $(":checkbox").attr("checked",false);
                            }
                            else
                                $.messager.alert('删除提示','删除信息失败，请稍后再试！','error');
                        },
                        //此处添加错误处理
                        error:function()
                        {
                            $.messager.alert('删除提示','删除信息异常，请稍后再试！','error');
                            return;
                        }
                    });
                }
            });
        }
    }

    //批量启用
    function setEnableFun() {
        var row = $('#tableData').datagrid('getChecked');
        if(row.length == 0)
        {
            $.messager.alert('启用提示','没有记录被选中！','info');
            return;
        }
        if(row.length > 0)
        {
            $.messager.confirm('启用确认','确定要启用选中的' + row.length + '条信息吗？',function(r)
            {
                if (r)
                {
                    var ids = "";
                    for(var i = 0;i < row.length; i ++)
                    {
                        if(i == row.length-1)
                        {
                            ids += row[i].id;
                            break;
                        }
                        ids += row[i].id + ",";
                    }
                    $.ajax({
                        type:"post",
                        url: path + "/bom/batchSetEnable.action",
                        dataType: "json",
                        async :  false,
                        data: ({
                            enabled: true,
                            BomIDs : ids,
                            clientIp: clientIp
                        }),
                        success: function (tipInfo)
                        {
                            var msg = tipInfo.showModel.msgTip;
                            if(msg == '成功')
                            {
                                //加载完以后重新初始化
                                $("#searchBtn").click();
                                $(":checkbox").attr("checked",false);
                            }
                            else
                                $.messager.alert('启用提示','启用信息失败，请稍后再试！','error');
                        },
                        //此处添加错误处理
                        error:function()
                        {
                            $.messager.alert('启用提示','启用信息异常，请稍后再试！','error');
                            return;
                        }
                    });
                }
            });
        }
    }

    //批量禁用
    function setDisEnableFun() {
        var row = $('#tableData').datagrid('getChecked');
        if(row.length == 0)
        {
            $.messager.alert('禁用提示','没有记录被选中！','info');
            return;
        }
        if(row.length > 0)
        {
            $.messager.confirm('禁用确认','确定要禁用选中的' + row.length + '条信息吗？',function(r)
            {
                if (r)
                {
                    var ids = "";
                    for(var i = 0;i < row.length; i ++)
                    {
                        if(i == row.length-1)
                        {
                            ids += row[i].id;
                            break;
                        }
                        ids += row[i].id + ",";
                    }
                    $.ajax({
                        type:"post",
                        url: path + "/bom/batchSetEnable.action",
                        dataType: "json",
                        async :  false,
                        data: ({
                            enabled: false,
                            BomIDs : ids,
                            clientIp: clientIp
                        }),
                        success: function (tipInfo)
                        {
                            var msg = tipInfo.showModel.msgTip;
                            if(msg == '成功')
                            {
                                //加载完以后重新初始化
                                $("#searchBtn").click();
                                $(":checkbox").attr("checked",false);
                            }
                            else
                                $.messager.alert('禁用提示','禁用信息失败，请稍后再试！','error');
                        },
                        //此处添加错误处理
                        error:function()
                        {
                            $.messager.alert('禁用提示','禁用信息异常，请稍后再试！','error');
                            return;
                        }
                    });
                }
            });
        }
    }

    //导入数据
    function setInputFun(){
        //IE下不允许编辑 input=file的值  解决思路：重新克隆input=file，把这个input元素复制一个，然后将原来的删除。
        //在IE下复制元素的时候，其中的值是不会被复制的，所以就达到了清空文件域的目的了。
        //而在Firefox下，其中的值也会被一同复制，清空一下就做到兼容
        var fileUploadInput = $("#BomFile");
        fileUploadInput.after(fileUploadInput.clone().val(""));
        fileUploadInput.remove();
        $("#isCheck").val(1);
        $('#importExcelDlg').dialog('open').dialog('setTitle','导入' + listType + '信息');
        $(".window-mask").css({ width: webW-20 ,height: webH});
        $("#BomFile").focus();
    }

    //导出数据
    function setOutputFun(){
        window.location.href = path + "/bom/exportExcel.action?browserType=" + getOs();
    }

    //增加单位
    var url;
    var BomID = 0;
    //保存编辑前的名称
    var orgBom = "";

    function addBom() {
        $('#BomDlg').dialog('open').dialog('setTitle','<img src="' + path + '/js/easyui-1.3.5/themes/icons/edit_add.png"/>&nbsp;增加'+listType+"信息");
        $(".window-mask").css({ width: webW ,height: webH});
        $("#item").focus();
        $('#BomFM').form('clear');
        orgBom = "";
        BomID = 0;
        url = path + '/bom/create.action';
    }

    function bindEvent(){

        //导入excel对话框
        $('#importExcelDlg').dialog({
            width: 400,
            closed: true,
            cache: false,
            modal: true,
            collapsible:false,
            closable: true,
            buttons:'#dlg-buttons5'
        });
        //导入excel表格
        $("#saveimport").unbind().bind({
            click:function()
            {
                if($("#BomFile").val().length == 0)
                {
                    $.messager.alert('提示','请选择文件！','info');
                    return;
                }
                $("#importExcelFM").submit();
                $('#importExcelDlg').dialog('close');

                $.messager.progress({
                    title:'请稍候',
                    msg:'数据处理ing...'
                });
                setTimeout(function(){
                    $.messager.progress('close');
                    var opts = $("#tableData").datagrid('options');
                    showBomDetails(opts.pageNumber,opts.pageSize);
                },3300);
            }
        });
		//保存信息
		$("#saveBom").off("click").on("click",function() {

			if(!$('#BomFM').form('validate')){
				return;
			}
			else if(checkBomName()){
				return;
			}
			
			$.ajax({
		        url: url,
		        type:"post",
		        dataType: "json",
		        data:{
		            item:$("#item").val(),
		            qty:$("#qty").val(),
		            ref_designator:$("#ref_designator").val(),
		            complete_part:$("#complete_part").val(),
		            description:$("#description").val(),
		            pakage_type_or_doc_type:$("#pakage_type_or_doc_type").val(),
		            value_or_rev:$("#value_or_rev").val(),
		            voltage:$("#voltage").val(),
		            tolerances:$("#tolerances").val(),
		            special_comments:$("#special_comments").val(),
		            rohs:$("#rohs").val(),
		            temperature_range:$("#temperature_rangen").val(),
		            manufacturer:$("#manufacturer").val(),
		            supplier:$("#supplier").val(),
		            supplier_part_no:$("#supplier_part_no").val(),
		            second_source_manufacturer:$("#second_source_manufacturer").val(),
		            second_source_part_no:$("#second_source_part_no").val(),
		            dr_rart_number:$("#dr_rart_number").val(),
		            enabled:1,
		            clientIp: clientIp
		     },
			 success: function (res){
				if(res){
					$('#BomDlg').dialog('close');
					var opts = $("#tableData").datagrid('options');
					
					showBomDetails(opts.pageNumber,opts.pageSize);
				}
			}
		});
	});
		 //初始化键盘enter事件
        $(document).keydown(function(event) {
            //兼容 IE和firefox 事件
            var e = window.event || event;
            var k = e.keyCode||e.which||e.charCode;
            //兼容 IE,firefox 兼容
            var obj = e.srcElement ? e.srcElement : e.target;
            //绑定键盘事件为 id是指定的输入框才可以触发键盘事件 13键盘事件
            if(k == "13"&&(obj.id=="item1" ))
            {
                $("#saveBom").click();
            }

            //搜索按钮添加快捷键
            if(k == "13"&&( obj.id=="item1"))
            {
                $("#searchBtn").click();
            }
        });
		 //搜索处理
        $("#searchBtn").unbind().bind({
            click:function()
            {
                showBomDetails(1,initPageSize);
                var opts = $("#tableData").datagrid('options');
                var pager = $("#tableData").datagrid('getPager');
                opts.pageNumber = 1;
                opts.pageSize = initPageSize;
                pager.pagination('refresh',
                    {
                        pageNumber:1,
                        pageSize:initPageSize
                    });
            }
        });

        $("#searchBtn").click();

        //重置按钮
        $("#searchResetBtn").unbind().bind({
            click:function(){
                $("#item1").val("");

                //加载完以后重新初始化
                $("#searchBtn").click();
            }
        });
        
        


    }
    //编辑信息
    function editBom(bomTotalInfo) {
        var BomInfo = bomTotalInfo.split("AaBb");
        var a=BomInfo[1];
        var row = {
        	item : a,
        	qty : BomInfo[2].replace("undefined",""),
        	ref_designator : BomInfo[3],
        	complete_part : BomInfo[4],
        	description : BomInfo[5],
        	pakage_type_or_doc_type : BomInfo[6],
        	value_or_rev : BomInfo[7],
        	voltage : BomInfo[8],
        	tolerances : BomInfo[9],
        	special_comments : BomInfo[10],
        	rohs : BomInfo[11],
        	temperature_range : BomInfo[12],
        	manufacturer : BomInfo[13],
        	supplier : BomInfo[14],
        	supplier_part_no : BomInfo[15],
        	second_source_manufacturer : BomInfo[16],
        	second_source_part_no : BomInfo[17],
        	dr_rart_number : BomInfo[18],
        	clientIp: clientIp
        };
 
        $('#BomDlg').dialog('open').dialog('setTitle','<img src="' + path + '/js/easyui-1.3.5/themes/icons/pencil.png"/>&nbsp;编辑'+listType +"信息");
        $(".window-mask").css({ width: webW ,height: webH});
        $('#BomFM').form('load',row);
        BomId = BomInfo[0];
        //焦点在名称输入框==定焦在输入文字后面
        $("#item").val("").focus().val(BomInfo[1]);
        url = path + '/bom/update.action?BomID=' + BomInfo[0];

        $.ajax({
            type:"post",
            url: path + "/depotHead/findTotalPay.action",
            dataType: "json",
            async:  false,
            data: ({
            	BomId: BomInfo[0],
                EndTime:thisDateTime,
                supType: supType
            }),
            success: function(res){
                if(res) {
                    var moneyA = res.getAllMoney.toFixed(2)-0;
                    $.ajax({
                        type:"post",
                        url: path +"/accountHead/findTotalPay.action",
                        dataType: "json",
                        async:  false,
                        data: ({
                        	BomId: BomInfo[0],
                            EndTime:thisDateTime,
                            supType: supType
                        }),
                        error: function(){
                            $.messager.alert('提示','网络异常请稍后再试！','error');
                            return;
                        }
                    });
                }
            },
            error: function(){
                $.messager.alert('提示','网络异常请稍后再试！','error');
                return;
            }
        })

    }
    //检查单位名称是否存在 ++ 重名无法提示问题需要跟进
    function checkBomName() {
        var bomName = $.trim($("#Bom").val());
        //表示是否存在 true == 存在 false = 不存在
        var flag = false;
        //开始ajax名称检验，不能重名
        if(bomName.length > 0 &&( orgBom.length ==0 || bomName != orgBom))
        {
            $.ajax({
                type:"post",
                url: path + "/bom/checkIsNameExist.action",
                dataType: "json",
                async :  false,
                data: ({
                    BomID : BomID,
                    Bom : BomName
                }),
                success: function (tipInfo)
                {
                    flag = tipInfo;
                    if(tipInfo)
                    {
                        $.messager.alert('提示','单位名称已经存在','info');
                        return;
                    }
                },
                //此处添加错误处理
                error:function()
                {
                    $.messager.alert('提示','检查单位名称是否存在异常，请稍后再试！','error');
                    return;
                }
            });
        }
        return flag;
    }
    function showBomDetails(pageNo,pageSize) {
        $.ajax({
            type:"post",
            url: path + "/bom/findBy.action",
            dataType: "json",
            data: ({
                item:$.trim($("#item1").val()),
                type: listType,
                pageNo:pageNo,
                pageSize:pageSize
            }),
            success: function (data)
            {
                $("#tableData").datagrid('loadData',data);
                //$('#tableData').datagrid('reload');
            },
            //此处添加错误处理
            error:function()
            {
                $.messager.alert('查询提示','查询数据后台异常，请稍后再试！','error');
                return;
            }
        });
    }
    
    function getToken(){
    	alert(1);
   	 	$.ajax({
            type:"post",
            url: path + "/bom/getData.action",
            dataType : 'json',
            async:  false,
            success: function (tipInfo)
            {
           	 
            },
            error: function(){
           	 
                return;
            }
        });
    	
    }
    